package JavaBase.Proxy;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class JDKProxySubject implements InvocationHandler {
    private Subject realSubject;

    public JDKProxySubject(Subject subject){
        this.realSubject = subject;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before");
        Object result = null;
        try{
            result = method.invoke(realSubject,args);
        }catch (Exception e){
            throw e;
        }finally {
            System.out.println("after");
        }

        return result;
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Constructor constructor = JDKProxySubject.class.getConstructor(Subject.class);

        JDKProxySubject jdkProxySubject = (JDKProxySubject) constructor.newInstance(new SubjectImpl());

    }
}
